package com.tms.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tms.basic.mapper.RoleMenuMapper;
import com.tms.basic.mapper.UserMapper;
import com.tms.basic.mapper.UserRoleMapper;
import com.tms.basic.pojo.domain.User;
import com.tms.basic.pojo.dto.UserDto;
import com.tms.basic.pojo.req.UserReq;
import com.tms.basic.pojo.vo.UserVo;
import com.tms.basic.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserRoleMapper userRoleMapper;
    
    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public UserVo getUserInfo(String username) {
        // 查询用户基本信息
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);
        User user = this.getOne(wrapper);
        
        if (user == null) {
            return null;
        }
        
        // 转换为UserVo对象
        UserVo userVo = convertToVo(user);
        
        // 获取用户角色列表（可以根据需要添加到UserVo中）
        List<String> roles = userRoleMapper.getRolesByUserId(user.getId());
        
        return userVo;
    }

    @Override
    public Page<UserVo> listUsers(UserReq userReq) {
        Page<User> page = new Page<>(userReq.getPageNum(), userReq.getPageSize());
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(userReq.getUsername()), User::getUsername, userReq.getUsername())
              .like(StringUtils.isNotBlank(userReq.getRealName()), User::getRealName, userReq.getRealName())
              .like(StringUtils.isNotBlank(userReq.getPhone()), User::getMobile, userReq.getPhone())
              .eq(userReq.getDepartmentId() != null, User::getDeptId, userReq.getDepartmentId())
              .eq(userReq.getPositionId() != null, User::getPositionId, userReq.getPositionId())
              .eq(userReq.getStatus() != null, User::getStatus, userReq.getStatus())
              .orderByDesc(User::getCreateTime);
        Page<User> userPage = this.page(page, wrapper);
        Page<UserVo> userVoPage = new Page<>();
        BeanUtils.copyProperties(userPage, userVoPage, "records");
        userVoPage.setRecords(userPage.getRecords().stream().map(this::convertToVo).toList());
        return userVoPage;
    }

    @Override
    public boolean createUser(UserDto userDto) {
        User user = new User();
        BeanUtils.copyProperties(userDto, user);
        return this.save(user);
    }

    @Override
    public boolean updateUser(UserDto userDto) {
        User user = new User();
        BeanUtils.copyProperties(userDto, user);
        return this.updateById(user);
    }

    @Override
    public boolean deleteUser(Long id) {
        return this.removeById(id);
    }

    @Override
    public boolean updateUserStatus(Long id, Boolean status) {
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(User::getId, id)
               .set(User::getStatus, status);
        return this.update(wrapper);
    }

    @Override
    public boolean resetUserPassword(Long id, String password) {
        LambdaUpdateWrapper<User> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(User::getId, id)
               .set(User::getPassword, password);
        return this.update(wrapper);
    }

    private UserVo convertToVo(User user) {
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(user, userVo);
        return userVo;
    }
}